package com.qf.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.Order;
import com.qf.pojo.SelectOrderAll;
import com.qf.pojo.User;
import com.qf.service.OrderService;
import com.qf.util.Basedata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/addOrder")
    public Basedata addOrder(HttpSession httpSession, Order order){

        User user = (User)httpSession.getAttribute("loginUser");
        if(user==null){
            return new Basedata(0,"请先登录");
        }
        order.setUid(user.getId());
        order.setId(null);

        Integer integer = orderService.addOrder(order);

        return new Basedata(1,"预定成功");
    }

    @RequestMapping("/selectAllOrder")
    public Basedata selectAllOrder(HttpSession httpSession,Integer page,Integer limit){

        User user = (User)httpSession.getAttribute("loginUser");
        Integer id = user.getId();

        PageHelper.startPage(page,limit);
        List<SelectOrderAll> list = orderService.selectAllOrder(id);

        PageInfo pageInfo = new PageInfo(list);

        Basedata basedata = new Basedata();
        basedata.setTotal(pageInfo.getTotal());
        basedata.setCode(1);
        basedata.setInfo(list);

        return basedata;
    }

    @RequestMapping("/delOrder")
    public Basedata delOrder(Integer id){
        Integer integer = orderService.delOrder(id);
        return new Basedata(1,"删除成功");
    }

}
